<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="deptName"
            placeholder="请输入作业工序"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="id"
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <el-col :span="20">
        <!-- 搜索 -->
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          v-show="showSearch"
          label-width="68px"
        >
          <el-form-item label="风险因素" prop="riskFactor">
            <el-input
              v-model="queryParams.riskFactor"
              placeholder="请输入风险因素"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="风险等级" prop="riskLevel">
            <el-select
              v-model="queryParams.riskLevel"
              placeholder="请选择风险等级"
              clearable
            >
              <el-option
                v-for="dict in dict.type.list_risk_level"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="隐患等级" prop="dangerLevel">
            <el-select
              v-model="queryParams.dangerLevel"
              placeholder="请选择隐患等级"
              clearable
            >
              <el-option
                v-for="dict in dict.type.list_danger_level"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
        <!-- 操作 -->
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['list:construction:add']"
              >新增</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['list:construction:remove']"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['list:construction:export']"
              >导出</el-button
            >
          </el-col>
          <right-toolbar
            :showSearch.sync="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </el-row>
        <!-- 表格 -->
        <el-table
          v-loading="loading"
          :data="constructionList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="作业工序" align="center" prop="typeNo" />
          <el-table-column
            label="风险标识"
            align="center"
            prop="riskRecognize"
          />
          <el-table-column label="风险因素" align="center" prop="riskFactor" />
          <el-table-column label="风险等级" align="center" prop="riskLevel">
            <template slot-scope="scope">
              <dict-tag
                :options="dict.type.list_risk_level"
                :value="scope.row.riskLevel"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="管控措施"
            align="center"
            prop="controlMeasure"
          />
          <el-table-column label="隐患等级" align="center" prop="dangerLevel">
            <template slot-scope="scope">
              <dict-tag
                :options="dict.type.list_danger_level"
                :value="scope.row.dangerLevel"
              />
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center" prop="status">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                active-value="0"
                inactive-value="1"
                @change="switchData(scope.row)"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column
            label="规范依据"
            align="center"
            prop="normativeBasis"
          />
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="LookUpdate(scope.row)"
                v-hasPermi="['list:construction:edit']"
                >查看</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['list:construction:edit']"
                >修改</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['list:construction:remove']"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <!-- 添加或修改施工清单对话框 -->
    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal="false" width="850px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12"
            ><el-form-item label="作业工序" prop="typeNo">
              <treeselect
                :disabled="disabled"
                v-model="form.typeNo"
                :options="deptOptions"
                :default-expand-level="100"
                placeholder="请选择作业工序"
              /> </el-form-item
          ></el-col>
          <el-col :span="12"
            ><el-form-item label="风险标识" prop="riskRecognize">
              <el-input
                :disabled="disabled"
                v-model="form.riskRecognize"
                placeholder="请输入风险标识"
              /> </el-form-item
          ></el-col>
        </el-row>

        <el-form-item label="风险因素" prop="riskFactor">
          <el-input
          :autosize="{ minRows: 3, maxRows: 10 }"
            maxlength="255"
            type="textarea"
            :disabled="disabled"
            v-model="form.riskFactor"
            placeholder="请输入风险因素"
          />
        </el-form-item>

        <el-row>
          <el-col :span="12"
            ><el-form-item label="风险等级" prop="riskLevel">
              <el-select
                :disabled="disabled"
                style="width: 305px"
                v-model="form.riskLevel"
                placeholder="请选择风险等级"
              >
                <el-option
                  v-for="dict in dict.type.list_risk_level"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="12"
            ><el-form-item label="隐患等级" prop="dangerLevel">
              <el-select
                :disabled="disabled"
                style="width: 305px"
                v-model="form.dangerLevel"
                placeholder="请选择隐患等级"
              >
                <el-option
                  v-for="dict in dict.type.list_danger_level"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
        </el-row>

        <el-form-item label="管控措施" prop="controlMeasure">
          <el-input
            :autosize="{ minRows: 3, maxRows: 10 }"
            maxlength="255"
            type="textarea"
            :disabled="disabled"
            v-model="form.controlMeasure"
            placeholder="请输入管控措施"
          />
        </el-form-item>

        <el-form-item label="规范依据" prop="normativeBasis">
          <el-input
            :autosize="{ minRows: 3, maxRows: 10 }"
            maxlength="255"
            type="textarea"
            :disabled="disabled"
            v-model="form.normativeBasis"
            placeholder="请输入规范依据"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            :disabled="disabled"
            v-model="form.remark"
            placeholder="请输入备注"
          />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number
            :disabled="disabled"
            v-model="form.sort"
            :min="1"
            :max="100"
            label="排序"
            style="width: 305px"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listConstruction,
  getConstruction,
  delConstruction,
  addConstruction,
  updateConstruction,
} from "@/api/list/construction";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { treeselect } from "@/api/list/type";
export default {
  name: "Construction",
  dicts: ["list_danger_level", "list_risk_level"],
  components: {
    Treeselect,
  },
  data() {
    return {
      //查看
      disabled: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 施工清单表格数据
      constructionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        typeNo: null,
        riskRecognize: null,
        riskFactor: null,
        riskLevel: null,
        controlMeasure: null,
        dangerLevel: null,
        normativeBasis: null,
        sort: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        typeNo: [
          {
            required: true,
            message: "请选择作业工序",
            trigger: "blur",
          },
        ],
        riskRecognize: [
          { required: true, message: "请输入风险标识", trigger: "blur" },
        ],
        riskFactor: [
          { required: true, message: "请输入风险因素", trigger: "blur" },
        ],
        riskLevel: [
          {
            required: true,
            message: "请选择风险等级",
            trigger: "change",
          },
        ],
        dangerLevel: [
          {
            required: true,
            message: "请选择隐患等级",
            trigger: "change",
          },
        ],
        sort: [
          {
            required: true,
            message: "请选择排序",
            trigger: "change",
          },
        ],
      },
      // 部门树选项
      deptOptions: undefined,
      // 部门名称
      deptName: undefined,
      defaultProps: {
        children: "children",
        label: "label",
      },
      typeNoData: {
        typeNo: 2,
      },
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
  },
  methods: {
    /** 查询施工清单列表 */
    getList() {
      this.loading = true;
      listConstruction(this.queryParams).then((response) => {
        this.constructionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      treeselect(this.typeNoData).then((response) => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.typeNo = data.id;
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        typeNo: null,
        riskRecognize: null,
        riskFactor: null,
        riskLevel: null,
        controlMeasure: null,
        dangerLevel: null,
        normativeBasis: null,
        remark: null,
        sort: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.typeNo = undefined;
      this.$refs.tree.setCurrentKey(null);
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.disabled = false;
      this.open = true;
      this.title = "设计清单一新增";
    },
    // 查看
    LookUpdate(row) {
      const id = row.id || this.ids;
      getConstruction(id).then((response) => {
        this.form = response.data;
        this.disabled = true;
        this.open = true;
        this.title = "设计清单一查看";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getConstruction(id).then((response) => {
        this.form = response.data;
        this.disabled = false;
        this.open = true;
        this.title = "设计清单一修改";
      });
    },
    //修改状态
    switchData(row) {
      var data = {
        status: row.status,
        id: row.id,
      };
      updateConstruction(data).then((response) => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateConstruction(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConstruction(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除施工清单编号为"' + ids + '"的数据项？')
        .then(function () {
          return delConstruction(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "list/construction/export",
        {
          ...this.queryParams,
        },
        `construction_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
